﻿using Microsoft.AspNetCore.Mvc;
using Single.Core.Service;
using Single.Sys.Entity;
using Single.WebApp.Components;

namespace Single.WebApp.Areas.Sys.Controllers
{
    [Area("Sys")]
    public class UserController : WorkController
    {
        public IActionResult Index()
        {
            ViewBag.PwdFormat = ServiceSysProps.SysConfig.GetContent("pwd_strategy_format");

            return View();
        }

        public IActionResult ChangePwd()
        {
            ViewBag.PwdFormat = ServiceSysProps.SysConfig.GetContent("pwd_strategy_format");

            return View();
        }

        public string Get(long id)
        {
            var entity = ServiceSysProps.SysUser.Get(id);

            return ServiceResult.SerializeData(entity, ignore: ["Password"]);
        }

        public string GetData(int pageNum, int pageSize, long orgId, string username, string name, string status)
        {
            var rows = ServiceSysProps.SysUser.GetPageList(pageNum, pageSize, orgId, username, name, status, out int total);

            return ServiceResult.SerializeData(new { rows, total }, ignore: ["Password"]);
        }

        public string Save(SysUser entity)
        {
            return ServiceSysProps.SysUser.InsertOrUpdate(entity).Serialize();
        }

        public string Delete(long[] ids)
        {
            return ServiceSysProps.SysUser.Delete(ids).Serialize();
        }

        public string GetRole()
        {
            var rows = ServiceSysProps.SysRole.GetAll();

            return ServiceResult.SerializeData(new { rows });
        }

        public string GetUserRole(long id)
        {
            var rows = ServiceSysProps.SysUserRole.GetByUserId(id);

            return ServiceResult.SerializeData(new { rows });
        }

        public string SetRole(long userId, List<SysUserRole> rows)
        {
            return ServiceSysProps.SysUserRole.SetRole(userId, rows).Serialize();
        }

        public string Reset(SysUser entity)
        {
            return ServiceSysProps.SysUser.ResetPassword(entity.Id, entity.Password).Serialize();
        }

        public string ChangePwdSave(long id, string oldPwd, string newPwd)
        {
            return ServiceSysProps.SysUser.ChangePassword(id, oldPwd, newPwd).Serialize();
        }
    }
}
